package com.suneee.quartz;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;

import com.suneee.base.util.ContextHelper;
import com.suneee.quartz.dao.ScheduleConfigDao;
import com.suneee.quartz.dao.ScheduleTimeDao;
import com.suneee.quartz.entity.ScheduleConfig;
import com.suneee.shop.dao.ShopDao;
import com.suneee.shop.entity.Shop;

/**
 * 订单同步servlet
 * @author shangwen
 *
 */
public class QuartzServlet extends HttpServlet{

	private static final long serialVersionUID = 6225057822701807953L;
	private static final Logger logger = LoggerFactory.getLogger(QuartzServlet.class);  
	
	@Autowired
	private ScheduleConfigDao scheduleConfigDao;
	
	@Autowired
	private ShopDao shopDao;
	
	@Autowired
	private ScheduleTimeDao scheduleTimeDao;
	
	@Override
	public void init() throws ServletException {
		scheduleConfigDao = ContextHelper.getBean("scheduleConfigDao", ScheduleConfigDao.class);
		shopDao       = ContextHelper.getBean("shopDao", ShopDao.class);
		scheduleTimeDao = ContextHelper.getBean("scheduleTimeDao", ScheduleTimeDao.class);
		//获取平台所有店铺
		List<Shop> shopList = shopDao.selectWhere(" status=? and enabled = 1 ", Shop.USE);
		ScheduleConfig dto = new ScheduleConfig();
		dto.setBusinessGroup("order");
		//执行订单调度任务
		for (Shop shop : shopList) {
			ScheduleConfig config = null;
			if(config == null){
				//首先根据店铺id去查找对应的配置信息
				dto.setShopId(shop.getShopId());
				config = scheduleConfigDao.getScheduleConfig(dto);
				if(config == null){
					//再根据企业编码查询配置信息
					dto.setShopId(null);
					dto.setEnterpriseCode(shop.getEnterpriseCode());
					config = scheduleConfigDao.getScheduleConfig(dto);
					//最后查询平台配置信息
					if(config == null){
						dto.setEnterpriseCode("suneee");
						config = scheduleConfigDao.getScheduleConfig(dto);
					}
				}
				//得到配置信息，根据配置信息执行任务调度 
				if(config != null && config.getInterva() != null){
					Map<String,String> jobMap = new HashMap<String, String>();
					//String jobNameStr = shop.getRuleInfo()+"_"+shop.getShopId()+"/"+shop.getEnterpriseCode()+"*"+"job"+"-"+shop.getProxyURL()+"|"+config.getBusinessGroup();
					String jobNameStr = shop.getShopId()+"_"+config.getBusinessGroup()+"_"+config.getConfigType();
					String jobGroupName = config.getBusinessGroup()+"_"+config.getConfigType()+"group" ;//任务组名
					String triggerName  = shop.getShopId()+"_"+config.getBusinessGroup()+"_"+config.getConfigType()+"trigger" ;//触发器名
					String triggerGroupName = shop.getShopId()+"triggerGroup";//触发器组名
					
					jobMap.put("shopId", shop.getShopId().toString());
					jobMap.put("interva", config.getInterva().toString());
					
					QuartzManager.addJob(jobNameStr,  
							jobGroupName, 
							triggerName, 
							triggerGroupName, 
							SynOrderQuartzJob.class, 
							//"0/"+config.getInterva()+" * * * * ?",
							"0 */"+config.getInterva()+" * * * ?",
							jobMap);
					//QuartzManager.startJobs();
				}
				config = null;
			}
	    }
		//再次循环执行其他调度任务
		//dto.setBusinessGroup("");
	}

	
//	@Override
//	public void init() throws ServletException {
//		int num =1;
//		for (int i = 0; i < 2; i++) {
//			
//			String jobName = "{\"appKey\":\"230558216666666\",\"secret\":\"a9fee0f44840016763d3c9a145f03982\",\"appSession\":\"6101412d8ad1c7d7578514ea0af07de36c44b30c04ae0072130474118\"}";//任务名
//			String jobGroupName = "jobGroupName"+num;//任务组名
//			String triggerName  = "triggerName"+num;//触发器名
//			String triggerGroupName ="triggerGroupName"+num;//触发器组名
//			QuartzManager.addJob(jobName, 
//					jobGroupName, 
//					triggerName, 
//					triggerGroupName, 
//					QuartzJob.class, 
//					"0/5 * * * * ?");
//			QuartzManager.startJobs();
//			num ++;
//		}
//	}
}
